package com.ecloud.arithmetic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.arithmetic.dao.EnergyTopMapper;
import com.ecloud.arithmetic.dto.EnergyTopDto;
import com.ecloud.arithmetic.dto.EnergyTopSelectDto;
import com.ecloud.arithmetic.entities.EnergyTop;
import com.ecloud.arithmetic.service.EnergyTopService;
import com.ecloud.common.ResultEnum;
import com.ecloud.common.exception.ServerException;
import com.ecloud.common.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class EnergyTopServiceImpl extends ServiceImpl<EnergyTopMapper, EnergyTop> implements EnergyTopService {
    @Autowired
    EnergyTopMapper mapper;

    @Override
    public void insert(EnergyTopDto dto) {
        EnergyTop et;
        try {
            et = dto.toEntities(new EnergyTop());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new ServerException(ResultEnum.ARGS_ERR, e.getMessage());
        }
        et.setDataMonth(TimeUtil.fromMonthStr(dto.getDataMonthStr()));
        et.setCreateTime(LocalDateTime.now());
        mapper.insert(et);
    }

    @Override
    public List<EnergyTop> select(EnergyTopSelectDto dto) {
        dto.toLimit();
        return mapper.selectPage(dto);
    }
}
